package com.weipu.common.facade.impl;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;

import com.weipu.common.DXApplication;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.CurrencyServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.CurrencyModel;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.facade.model.CurrencyParmars;
import com.weipu.common.okhttp.OkHttpHelper;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;
import com.weipu.common.util.NetWorkUtil;

/**
 * @author tony 多货币相关接口实现类
 */
public class CurrencyServiceProviderImpl extends BaseServiceProviderImpl
		implements CurrencyServiceProvider {

	private static final String TAG = "CurrencyServiceProviderImpl";

	// private static Map<String, String> mapUrl;
	//
	// //方法名与URL的绑定
	// static{
	// mapUrl = new HashMap<String, String>();
	// mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_GETCURRENCYCODES,
	// Constant.GETCURRENCYCODES_URL);
	// mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_SWITCHPRODUCTCURRENCY,
	// Constant.SWITCHPRODUCTCURRENCY_URL);
	// mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_SWITCHCURRENCYCODE,
	// Constant.SWITCHCURRENCYCODE_URL);
	// }


	@Override
	public ArrayList<CurrencyModel> getCurrencyCodes() throws DxException {
		ArrayList<CurrencyModel> currencyList = null;
		JSONObject data = requestHttpGetData(Constant.GETCURRENCYCODES_URL);
		if (data != null) {
			JSONArray ja = data.optJSONArray("CurrencyCodes");
			if (ja != null && ja.length() > 0) {
				currencyList = new ArrayList<CurrencyModel>();
				for (int i = 0; i < ja.length(); i++) {
					JSONObject object = ja.optJSONObject(i);
					CurrencyModel model = CurrencyModel.json2Model(object);
					if (model != null) {
						currencyList.add(model);
					}
				}
			}
		}
		return currencyList;
	}

	@Override
	public ArrayList<ProductModel> switchCurrencyCode(ArrayList<Integer> skus,
			String currencyCode) throws DxException {
		ArrayList<ProductModel> products = new ArrayList<ProductModel>();
		CurrencyParmars parmars = new CurrencyParmars();
		parmars.setCurrencyCode(currencyCode);
		parmars.setSkus(skus);
		JSONObject data = requestHttpPostData(Constant.SWITCHCURRENCYCODE_URL,
				parmars);
		JSONArray jsonArray = data.optJSONArray("Products");
		ProductModel product;
		if (jsonArray != null && jsonArray.length() != 0) {
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject2 = jsonArray.optJSONObject(i);
				if (Constant.BFD_DEBUGMODEL) {
					Log.d(TAG, jsonObject2.toString());
				}
				product = ProductModel.json2product(jsonObject2);
				products.add(product);
			}
		}
		return products;
	}

	@Override
	public ArrayList<ProductModel> switchProductCurrency(
			ArrayList<Integer> skus, String currencyCode,int pageIndex,
			int pageSize,String orderbyField,long customerID,boolean isFilter,String categoryIds) throws DxException {
		ArrayList<ProductModel> products = new ArrayList<ProductModel>();
		CurrencyParmars parmars = new CurrencyParmars();
		parmars.setCurrencyCode(currencyCode);
		parmars.setSkus(skus);
		parmars.setCustomerID(customerID);
		parmars.setFilter(isFilter);
		parmars.setCategoryIds(categoryIds);
		parmars.setPageIndex(pageIndex);
		parmars.setPageSize(pageSize);
		parmars.setOrderbyField(orderbyField);
		JSONObject data = requestHttpPostData(Constant.SWITCHPRODUCTCURRENCY_URL,
				parmars);
		JSONArray jsonArray = data.optJSONArray("Products");
		String mCurrencyCode = data.optString("CurrencyCode");
		String mCurrencyCodeSymbol = data.optString("CurrencyCodeSymbol");
		ProductModel product;
		if (jsonArray != null && jsonArray.length() != 0) {
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject2 = jsonArray.optJSONObject(i);
				if (Constant.BFD_DEBUGMODEL) {
					Log.d(TAG, jsonObject2.toString());
				}
				product = ProductModel.json2product(jsonObject2);
				if (!TextUtils.isEmpty(mCurrencyCode)) {
					product.setCurrencyCode(mCurrencyCode);
				}
				if (!TextUtils.isEmpty(mCurrencyCodeSymbol)) {
					product.setCurrencyCodeSymbol(mCurrencyCodeSymbol);
				}
				products.add(product);
			}
		}
		return products;
	}

}
